// packageE/solitaire/components/yz_goodsPopup/yz_goodsPopup.js
var app = getApp();
var specsMy = require("../../../../mybehaviors/specs");
const cartPost = require('../cart');
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');

Component({
  behaviors: [specsMy, cartPost, skinBehavior],
  /**
   * 组件的属性列表
   */
  properties: {
    good_id: {
      value: '',
      type: String
    },
    addCartBtn: {
      value: false,
      type: Boolean
    },
    isScanCode: {
      value: false,
      type: Boolean
    },
    store_id: {
      value: '',
      type: String
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    language: {},
    getMarketPrice: '',
    timeData: {},
    themeColor: '#f14e4e',
    show: false,
    commentPage: 1,
    manySpecsShow: false
  },
  observers: {
    'good_id': function (showA) {
      if (showA) {
        console.log('组件', showA)
        this.getData();
      }
    }
  },
  lifetimes: {
    attached() {
      this.setData({
        manySpecsShow: false
      })
    },
    ready() {
      try {
        wx.getStorage({
          key: 'langIndex',
          success: (res) => {
            console.log(res.data, 888);
            this.setData({ 'language': res.data.en });
          }
        });
        wx.getStorage({
          key: 'yz_basic_info',
          success: (res) => {
            this.setData({
              themeColor: res.data.theme_color,
              getMarketPrice: res.data.lang.goods && res.data.lang.goods.market_price ? res.data.lang.goods.market_price : '',
              getprice: res.data.lang.goods && res.data.lang.goods.price ? res.data.lang.goods.price : ''
            });
          }
        });
      }
      catch (e) {
        console.log(e);
      }
    }
  },

  /**
  * 组件的方法列表
  */
  methods: {
    ShowspecsTap() {
      this.setData({
        manySpecsShow: true
      })
    },
    onClosemanySpecs(){
      this.setData({
        manySpecsShow: false
      })
    },
    changeStepper(e) {
      let { id } = e.currentTarget.dataset;
      console.log(id)
      this.addCart(id, this.data.store_id, '', true)
    },
    updateCart() {
      this.triggerEvent("updateCart")
      this.getCart();
    },
    closeCart() {
      // 到预下单页
      this.triggerEvent('toOrderChild')
    },
    getData() {
      let urlStr = app.getNetAddresss(
        "goods.goods.get-goods-page"
      );
      app._getNetWork({
        url: urlStr,
        data: {
          id: this.data.good_id
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            console.log(res.data)
            this.setData({
              goodsInfo: res.data.get_goods,
              all_goodsInfo: res.data,
              goods_plugin: res.data.plugin,
              show: true
            })
            this.thisSpecsSetData();
            this.getCart();
          } else {
            app.tips(res.msg)
          }
        },
        fail: function (res) { },
      });
    },
    thisSpecsSetData() {
      let language = wx.getStorageSync('langIndex');
      this.setData({
        'language': language.en,
        specsManage: [],
        optionsId: 0,
        goodsCount: 1
      });
      if (this.data.isScanCode) {
        setTimeout(() => {
          this.initPopView();
        }, 100);
      }
      if (this.data.goodsInfo.member_discount && this.data.goodsInfo.member_discount.discount) {
        if (app._isTextEmpty(this.data.goodsInfo.member_discount.discount)) {
          this.setData({
            openVip: 1,
          });
        } else {
          this.setData({
            openVip: this.data.goodsInfo.member_discount.discount,
          });
        }
        if (this.data.goodsInfo.member_discount.all_level_price) this.getMemLevelInfo(this.data.goodsInfo);
      }
    },
    getMemLevelInfo(goodsInfo) {
      let lastMemLevelInfo, activeMemLevelInfo = {};
      goodsInfo.member_discount.all_level_price.forEach(element => {
        if (element.is_select) activeMemLevelInfo = element;//当前会员等级优惠信息
        if (element.is_next) lastMemLevelInfo = element;//下一个会员等级优惠信息
      });
      this.setData({
        activeMemLevelInfo, lastMemLevelInfo
      })
    },
    gotoMemberGradeList(e) {
      let tag = e.currentTarget.dataset.flag;
      if (tag == "1") {
        wx.navigateTo({
          url: "/packageB/member/MemberGradeList/MemberGradeList",
        });
      }
    },
    storeAddCart() {
      this.triggerEvent('addCart', { goodsInfo: this.data.goodsInfo, optionsId: this.data.optionsId, goodsCount: this.data.goodsCount });
    },
    goodsCountBlur(e) {
      this.setData({
        goodsCount: Number(e.detail.value)
      })
    },
    popupSpecsbtn(evt) {
      console.log(evt);
      this.triggerEvent("closeShow")
      this.setData({ show: false });
    },
    onChangeTime(e) {
      this.setData({
        timeData: e.detail,
      });
    },
    // addCart() {
    //   this.triggerEvent('addCart', this.data.goodsInfo);
    // }
  }
});















